package cl.hhha.web.webapp.action;

import com.opensymphony.xwork2.Preparable;
import cl.hhha.web.service.GenericManager;
import cl.hhha.web.model.OrientacionFormulario;
import cl.hhha.web.webapp.action.BaseAction;

import java.util.List;

public class OrientacionFormularioAction extends BaseAction implements Preparable {
    private GenericManager<OrientacionFormulario, Long> orientacionFormularioManager;
    private List orientacionFormularioes;
    private OrientacionFormulario orientacionFormulario;
    private Long  id;

    public void setOrientacionFormularioManager(GenericManager<OrientacionFormulario, Long> orientacionFormularioManager) {
        this.orientacionFormularioManager = orientacionFormularioManager;
    }

    public List getOrientacionFormularioes() {
        return orientacionFormularioes;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String orientacionFormularioId = getRequest().getParameter("orientacionFormulario.id");
            if (orientacionFormularioId != null && !orientacionFormularioId.equals("")) {
                orientacionFormulario = orientacionFormularioManager.get(new Long(orientacionFormularioId));
            }
        }
    }

    public String list() {
        orientacionFormularioes = orientacionFormularioManager.getAll();
        return SUCCESS;
    }

    public void setId(Long  id) {
        this. id =  id;
    }

    public OrientacionFormulario getOrientacionFormulario() {
        return orientacionFormulario;
    }

    public void setOrientacionFormulario(OrientacionFormulario orientacionFormulario) {
        this.orientacionFormulario = orientacionFormulario;
    }

    public String delete() {
        orientacionFormularioManager.remove(orientacionFormulario.getId());
        saveMessage(getText("orientacionFormulario.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (id != null) {
            orientacionFormulario = orientacionFormularioManager.get(id);
        } else {
            orientacionFormulario = new OrientacionFormulario();
        }

        return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (orientacionFormulario.getId() == null);

        orientacionFormularioManager.save(orientacionFormulario);

        String key = (isNew) ? "orientacionFormulario.added" : "orientacionFormulario.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }
}